#include "hal_tm1650.h"
#include "bsp.h"

static void SDA_IN(void)
{
  gpio_init(TM1650_SDA_GPIO_Port, 					GPIO_MODE_IPU, 	GPIO_OSPEED_50MHZ,	TM1650_SDA_Pin);//TM1650
}
static void SDA_OUT(void)
{
	gpio_init(TM1650_SDA_GPIO_Port, 					GPIO_MODE_OUT_PP, 	GPIO_OSPEED_50MHZ,	TM1650_SDA_Pin);//TM1650
}
void TM1650_delay_us(unsigned short j)
{
  unsigned short i,k;
  for(k=0;k<j;k++)
  {
    for(i=0;i<1;i++)
    {
    }
  }
}


void TM1650_Start(void)
{
	SDA_OUT();     
	TM1650_SDA_H;	  	  
	TM1650_SCL_H;
	TM1650_delay_us(4);
 	TM1650_SDA_L;
	TM1650_delay_us(4);
	TM1650_SCL_L;
}


void TM1650_Stop(void)
{
	SDA_OUT();
	TM1650_SCL_L;
	TM1650_SDA_L;
 	TM1650_delay_us(4);
	TM1650_SCL_H; 
	TM1650_delay_us(4);
	TM1650_SDA_H;
					   	
}

unsigned char  TM1650_Wait_Ack(void)
{
	unsigned char  ucErrTime=0;
	SDA_IN();       
	TM1650_SDA_H;TM1650_delay_us(1);	   
	TM1650_SCL_H;TM1650_delay_us(1);	 
	while(READ_SDA)
	{
		ucErrTime++;
		if(ucErrTime>10)  //这里之前是250 现在改为10
		{
			TM1650_Stop();
			return 1;
		}
	}
	TM1650_SCL_L;	   
	return 0;  
} 

void TM1650_Ack(void)
{
	TM1650_SCL_L;
	SDA_OUT();
	TM1650_SDA_L;
	TM1650_delay_us(4);
	TM1650_SCL_H;
	TM1650_delay_us(4);
	TM1650_SCL_L;
}
	    
void TM1650_NAck(void)
{
	TM1650_SCL_L;
	SDA_OUT();
	TM1650_SDA_H;
	TM1650_delay_us(4);
	TM1650_SCL_H;
	TM1650_delay_us(4);
	TM1650_SCL_L;
}					 				     
		
void TM1650_Send_Byte(unsigned char  oneByte)
{                        
    unsigned char  t;   
    SDA_OUT(); 	    
    TM1650_SCL_L;
    for(t=0;t<8;t++)
    {              
        if((oneByte&0x80)==0x80)   TM1650_SDA_H;
					else	                   TM1650_SDA_L;
        oneByte<<=1; 	  
		TM1650_delay_us(4);   
		TM1650_SCL_H;
		TM1650_delay_us(4); 
		TM1650_SCL_L;	
		TM1650_delay_us(4);
    }	 
}


unsigned char  TM1650_Read_Byte(void)
{
	unsigned char i,rekey=0;
	SDA_IN();
	for(i=0;i<8;i++ )
	{
		TM1650_SCL_L; 
		TM1650_delay_us(4);
		TM1650_SCL_H;
		rekey<<=1;
		if(READ_SDA) rekey++;   
		TM1650_delay_us(4); 
	}			
	return rekey;
}

void TM1650_SendCommand(unsigned char add,unsigned char dat)
{	
  TM1650_Start();
	TM1650_Send_Byte(add);
	TM1650_Wait_Ack();
	TM1650_Send_Byte(dat);
	TM1650_Wait_Ack();
	TM1650_Stop(); 
}

void TM1650_Init(void)
{
	#if(QIANG_YING_DEVICE == 1)	//如果是强饮
	TM1650_SCL_H;
	TM1650_SDA_H;
	TM1650_SendCommand(0X48,0X01);
	#endif
}

void DisplayNumber_4BitDig(uint16_t Num)
{	
//	uint16_t Numb;
//	Numb = Num+10000;
//	TM1650_SendDigData(1,Numb/1000%10);
//	TM1650_SendDigData(2,Num/100%10);	
//	TM1650_SendDigData(3,Num/10%10);
//	TM1650_SendDigData(4,Num%10);
	
}
//uint32_t TM1650_Gte_KEY(void)
//{
////unsigned char key;
////	key=TM1650_Read_KEY();
////	uint32_t key_name,i,j;
////	for(i=0;i<7;i++)
////		for(j=0;j<7;j++)
////		{
////			if(key == key_numberH[i][j])
////			{
////				key_name = key_number[i][j];
////				return key_name;
////			}
////		}
////	
////	return key_name;
//	
//}

//段码数据表
const volatile uint8_t Digital_Table[19] = { 
0x3F, //0
0x06, //1
0x5B, //2
0x4F, //3
0x66, //4
0x6D, //5
0x7D, //6
0x07, //7
0x7F, //8
0x6F, //9
0x00, //'null '
0x48, //= 咖啡和茶 有效
0X79,//E  茶过期
0X39,//C  茶5泡完成
0x73,  //P
0x38,//L
0x06, //错误1
0x5B, //错误2
0x4F, //错误3
};	


uint8_t  g_LED_type[4] ={0};
//写入数码管的四位变量
uint8_t Dig1_Data=0;
uint8_t Dig2_Data=0;
uint8_t Dig3_Data=0;
uint8_t Dig4_Data=0;	
	
//=============================================================================
//Function:	TM1650_SendCommand  
//note: Set TM1650 Command1&Command2
//Input:		CMD1,数据命令:48H。
//					CMD2,开显示，显示亮度级
//Output:	none
//Remark: none
////=============================================================================
//void TM1650_SendCommand(u8 CMD1,u8 CMD2)
//{	
//    TM_IIC_Start();
//	TM_IIC_Send_Byte(CMD1);
//	TM_IIC_Wait_Ack();
//	TM_IIC_Send_Byte(CMD2);
//	TM_IIC_Wait_Ack();
//	TM_IIC_Stop(); 
//}

#if (QIANG_YING_DEVICE == 1)	//如果是强饮
void LED_ON(teTM1650Led led)
{
		switch(led)
		{
			case LED_LOCK_LIGHT: g_LED_type[1] |= 0x02;break;//童锁
			case LED_LOCK_FLAG1: g_LED_type[3] |= 0x01;break;//童锁1
			case LED_LOCK_FLAG2: g_LED_type[3] |= 0x04;break;//童锁2
			case LED_LOCK_FLAG3: g_LED_type[3] |= 0x02;break;//童锁3
			
			case LED_HOT_LIGHT:  g_LED_type[1] |= 0x01;break;//热水
			case LED_HOT_FLAG1:  g_LED_type[2] |= 0x01;break;//热水1
			case LED_HOT_FLAG2:  g_LED_type[2] |= 0x04;break;//热水2
			case LED_HOT_FLAG3:  g_LED_type[2] |= 0x02;break;//热水3
			
			default:break;
		}
}
void LED_OFF(teTM1650Led led)
{
			switch(led)
		{
			case LED_LOCK_LIGHT: g_LED_type[1] &= (~0x02);break;//童锁
			case LED_LOCK_FLAG1: g_LED_type[3] &= (~0x01);break;//童锁1
			case LED_LOCK_FLAG2: g_LED_type[3] &= (~0x04);break;//童锁2
			case LED_LOCK_FLAG3: g_LED_type[3] &= (~0x02);break;//童锁3
			
			case LED_HOT_LIGHT:  g_LED_type[1] &= (~0x01);break;//热水
			case LED_HOT_FLAG1:  g_LED_type[2] &= (~0x01);break;//热水1
			case LED_HOT_FLAG2:  g_LED_type[2] &= (~0x04);break;//热水2
			case LED_HOT_FLAG3:  g_LED_type[2] &= (~0x02);break;//热水3

			default:break;
		}
}
#else
void LED_ON(teTM1650Led led)
{
		switch(led)
		{
			case LED_HOT_LIGHT: HAL_GPIO_WritePin(KEY_LOCK_GPIO_Port,KEY_LOCK_Pin, 1); //童锁按键;break;//热水
			default:break;
		}
}
void LED_OFF(teTM1650Led led)
{
			switch(led)
		{
			case LED_HOT_LIGHT: HAL_GPIO_WritePin(KEY_LOCK_GPIO_Port,KEY_LOCK_Pin, 0); //童锁按键;break;//热水
			

			default:break;
		}
}

#endif


void LED8_display(uint8_t num)
{
  Dig1_Data=Digital_Table[num];
}

//=============================================================================
//Function:	DisplayNumber_4BitDig  
//note: 	设置4位数码管数据
//Input:	显示的4位数据
//Output:	none
//Remark: none
//=============================================================================
// void DisplayNumber_4BitDig(u16 num,u16 led)   //@7 NUM 代表数码管  led 代表灯的显示
// {
// 	  // Dig1_Data=Digital_Table[num];
// 	  // Dig2_Data=led;
	
// //	  Dig1_Data=Digital_Table[num/1000];
// //	Dig2_Data=Digital_Table[num/100%10];	
// //	Dig3_Data=Digital_Table[num/10%10];
// //	Dig4_Data=Digital_Table[num%10];
// }

//=============================================================================
//Function:	DigitalScan  
//note: 	数码管扫描
//Input:	none
//Output:	none
//Remark: none
//=============================================================================
void DigitalScan(void)  
{

//	Aip650_SendDigData(DIG0,Dig1_Data);
//	Aip650_SendDigData(DIG0,g_LED_type);  //@7 显示 led
//  Aip650_SendDigData(DIG1,Dig2_Data);
//	Aip650_SendDigData(DIG2,Dig3_Data);
//	Aip650_SendDigData(DIG3,Dig4_Data);	
	  TM1650_SendCommand(DIG2,g_LED_type[1]);
		TM1650_SendCommand(DIG3,g_LED_type[2]);
		TM1650_SendCommand(DIG4,g_LED_type[3]);
}

unsigned char TM1650_Read_KEY(void)
{	
	unsigned char temp;
  TM1650_Start();
	TM1650_Send_Byte(0x49);
	TM1650_Wait_Ack();
	temp=TM1650_Read_Byte();
	TM1650_Wait_Ack();
	TM1650_Stop();
	return temp;
}



////=============================================================================
////Function:	TM1650_Read_KEY  
////note: 读按键数据
////Input:	none
////Output:	键盘扫描码
////Remark: none
////=============================================================================
uint8_t KEY_Scan(void)
{	
	#if(QIANG_YING_DEVICE == 1)	//如果是强饮
	uint8_t kv=TM1650_Read_KEY();
  switch(kv)
	{
		case KEY_WASH:
		{
//			printf("KEY_WASH=%X\r\n",kv);
		}break;
		case KEY_HEAT:
		{
//			printf("KEY_HEAT=%X\r\n",kv);
		}break;
		default:break;
	}
	return kv;
#endif
}

void hal_tm1650_test(void)
{

	LED_ON(LED_LOCK_LIGHT );
	LED_ON(LED_LOCK_FLAG1 );
	LED_ON(LED_LOCK_FLAG2 );
	LED_ON(LED_LOCK_FLAG3 );

	LED_ON(LED_HOT_LIGHT  );
	LED_ON(LED_HOT_FLAG1  );
	LED_ON(LED_HOT_FLAG2  );
	LED_ON(LED_HOT_FLAG3  );
	
//	LED_ON(LED_LOCK_FLAG2);
 //  TM1650_Send_Byte(11);
//	TM1650_SendCommand(11,22);
//	  static uint8_t i = 0;
//    if(i==0)
//		{
//			LED_ON(0Xff);
//			i++;
//		}
//		else 
//		{
//				LED_OFF(0X0f);
//			  i=0;
//		}
}
